package com.eascs.web.p2p.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UuidUtil;
import com.eascs.p2p.common.service.IAdminRoleService;
import com.eascs.p2p.common.service.InvestorService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.p2p.utils.StringUtils;

@Controller
@RequestMapping("/common")
public class CommonController extends BaseRestController {
	@Reference
	InvestorService investorService;
	@Reference
	IAdminRoleService adminRoleService;
	
	@Value("${web.upload.path}")
	private String uploadPath;
	
	@Value("${web.image.domain}")
	private String imageDomain;		//图片服务器域名

	@RequestMapping(value = "/searchInvestorName")
	public BaseRestResult<List<Object[]>> searchInvestorName(String keywords) {
		ModelResult<List<Object[]>> model = investorService.getInvestorNameorMobile(keywords);
		List<Object[]> data = new ArrayList<Object[]>();
		if (model.isSuccess()) {
			data = model.getModel();
		}
		if (org.springframework.util.StringUtils.hasText(keywords)) {
			data.add(new Object[] { UuidUtil.generateShortUuid(), keywords + UuidUtil.generateShortUuid() });
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	@RequestMapping(value = "/searchRoleName")
	public BaseRestResult<List<Object[]>> searchRoleName(String keywords) {
		ModelResult<List<Object[]>> model = adminRoleService.getRoleName(keywords);
		List<Object[]> data = new ArrayList<Object[]>();
		if (model.isSuccess()) {
			data = model.getModel();
		}
		if (org.springframework.util.StringUtils.hasText(keywords)) {
			data.add(new Object[] { UuidUtil.generateShortUuid(), keywords + UuidUtil.generateShortUuid() });
		}
		return BaseRestResult.buildRestResult(data);
	}


	@RequestMapping(value = "/upload/{type}")
	public void fileUpload(@RequestParam(value = "imgFile", required = false) MultipartFile localUrl,
			@PathVariable("type") String type,HttpServletResponse  response) throws Exception {
		
		String fileName = localUrl.getOriginalFilename();
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();// 后缀名
		//新文件名称
		String newfilename = System.currentTimeMillis() + StringUtils.getRandomString(2) + "." + suffix;
		
		String spr = File.separator;								//系统文件分隔符
		//文件相对路径(type加上4位数的年月)
		String relatePath = type + spr + StringUtils.getFourBitYearMonth(new Date());
		String filePath = uploadPath + relatePath+spr;	
		String imageUrl = imageDomain +spr+ relatePath +spr+ newfilename;	//图片url
		String url= imageUrl.replaceAll("\\\\", "/");
		FileUtils.writeByteArrayToFile(new File(filePath + newfilename), localUrl.getBytes());
		JSONObject obj = new JSONObject();

		obj.put("error", 0);

		obj.put("url", url);

		response.getWriter().write(obj.toJSONString());
	}
}
